﻿namespace IntellectShop.Web2.Controllers
{
	using System;
	using System.Collections.Generic;
	using System.Web;
	using System.Web.Mvc;
	using IntellectShop.Common;
	using IntellectShop.Web2.ViewModels;

	[IntellectShop.Web2.Code.NoCache]
	[IntellectShop.Web2.Code.HandleErrorWithELMAH]
	[ValidateInputAttribute(false)]
	public class CertificationController : Controller
	{
		public ActionResult Browse(string categoryID)
		{
			int id = Int32.Parse(categoryID);
			var category = Common.Category.RetrieveByID(id);
			if (category == null)
				throw new HttpException(404, "Page not found");

			var viewModel = new CategoryBrowseCertificationViewModel
			{
				Category = category,
				Certifications = Common.Certification.RetrieveByCategoryID(id)
			};

			return View(viewModel);
		}

		public ActionResult CategoryIndex()
		{
			List<Common.Category> categories = Common.Certification.RetrieveCategoriesAll();

			var viewModel = new StoreCategoryIndexViewModel
			{
				NumberOfCategories = categories.Count,
				Categories = categories
			};

			return View(viewModel);
		}

		public ActionResult Details(int id)
		{
			var certification = Common.Certification.RetrieveByCommodityID(id);
			if (certification == null)
				throw new HttpException(404, "Page not found");

			return View(certification);
		}

		public ActionResult Index()
		{
			var viewModel = new CertificationIndexViewModel
			{
				Certifications = Common.Certification.RetrieveAll()
			};

			return View(viewModel);
		}

		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Create()
		{
			var viewModel = new CertificationEditViewModel
			{
				Categories = Common.Category.RetrieveAll(),
				Certification = new Common.Certification()
			};

			return View(viewModel);
		}

		[Authorize(Roles = Globals.AdministratorRoleName)]
		[HttpPost]
		public ActionResult Create(Common.Certification certification)
		{
			try
			{
				if (!ModelState.IsValid)
					throw new ArgumentException("Ошибка при заполнении полей для: Certification!");

				certification.Create(HttpContext.User.Identity.Name);
				return RedirectToAction("Index");
			}
			catch (Exception e)
			{
				var viewModel = new CertificationEditViewModel
				{
					Categories = Common.Category.RetrieveAll(),
					Certification = certification,
					Message = e.Message
				};

				return View(viewModel);
			}
		}

		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Edit(int id)
		{
			var certification = Common.Certification.RetrieveByCommodityID(id);
			if (certification == null)
				throw new HttpException(404, "Page not found");

			var viewModel = new CertificationEditViewModel
			{
				Categories = Common.Category.RetrieveAll(),
				Certification = certification
			};

			return View(viewModel);
		}

		[Authorize(Roles = Globals.AdministratorRoleName)]
		[HttpPost]
		public ActionResult Edit(int id, FormCollection collection)
		{
			var certification = Common.Certification.RetrieveByCommodityID(id);
			if (certification == null)
				throw new HttpException(404, "Page not found");

			try
			{
				if (!ModelState.IsValid)
					throw new ArgumentException("Ошибка при заполнении полей для: Certification!");

				UpdateModel(certification, "Certification");
				certification.Update(HttpContext.User.Identity.Name);
				return Redirect(Url.RouteUrl(new { action = "Index" }) + "#id" + id);
			}
			catch (Exception e)
			{
				var viewModel = new CertificationEditViewModel
				{
					Certification = certification,
					Categories = Common.Category.RetrieveAll(),
					Message = e.Message
				};

				return View(viewModel);
			}
		}

		[Authorize(Roles = Globals.AdministratorRoleName)]
		public ActionResult Delete(int certificationID)
		{
			var certification = Common.Certification.RetrieveByCommodityID(certificationID);
			if (certification == null)
				throw new HttpException(404, "Page not found");

			var viewModel = new CertificationRemoveViewModel()
			{
				Certification = certification
			};

			return View(viewModel);
		}

		[Authorize(Roles = Globals.AdministratorRoleName)]
		[HttpPost]
		public ActionResult Delete(int certificationID, FormCollection collection)
		{
			var certification = Common.Certification.RetrieveByCommodityID(certificationID);
			if (certification == null)
				throw new HttpException(404, "Page not found");

			try
			{
				if (!ModelState.IsValid)
					throw new ArgumentException("Ошибка при заполнении полей для: Certification!");

				Common.Certification.Delete(certificationID);
				return RedirectToAction("Index");
			}
			catch (Exception e)
			{
				var viewModel = new CertificationRemoveViewModel()
				{
					Certification = certification,
					Message = e.Message
				};

				return View(viewModel);
			}
		}
	}
}
